/*pmFWCFu5nhKkBzYmrkBakYsZp5swriLu08OGCHqhiBP8j8oYSTPG1fSFojrU7cKi*/
/*
 * @Author: xiahui 
 * @PageInfo: 拉价格审批单模板初始化
 * @Date: 2019-04-29 13:48:19
 * @Last Modified by: yangls7
 * @Last Modified time: 2019-08-22 09:27:59
 */
import { APPCODE, PAGECODE, AREA, BUTTONID, FIELDS, DATASOURCECACHE, DEFCACHEKEY } from '../../constance';
import { setRefMainOrgPermissonByItem } from '../../../pub/utils/referUtil';
import { setPsndocShowLeavePower, setRefShowDisabledData } from '../../../../../scmpub/scmpub/pub/tool';
import { transferSkipToSrcBillUtil } from '../../../../../scmpub/scmpub/pub/tool/transferSkipToSrcBillUtil';
import { getDefData } from '../../../../../scmpub/scmpub/pub/cache';

export default function(props) {
	props.createUIDom(
		{
			pagecode: PAGECODE.ref28Pagecode,
			appcode: APPCODE.appcode28
		},
		(data) => {
			if (data) {
				if (data.context) {
					this.contexts = data.context;
					props.BillHeadInfo.setBillHeadInfoVisible({
						showBackBtn: true, //控制显示返回按钮: true为显示,false为隐藏 ---非必传
						showBillCode: false, //控制显示单据号：true为显示,false为隐藏 ---非必传
						billCode: null //修改单据号---非必传
					});
				}
				if (data.button) {
					let button = data.button;
					props.button.setButtons(button);
					// 刷新按钮
					let cacheData = getDefData(DATASOURCECACHE.dataSourceRef28CacheKey, DEFCACHEKEY.query28CacheKey);
					props.button.setDisabled(BUTTONID.Refresh, cacheData ? false : true);
				}
				if (data.template) {
					let meta = data.template;
					modifierMeta(props, meta);
					props.meta.setMeta(meta);
				}
			}
		}
	);
}

function modifierMeta(props, meta) {
	// 查询区参照过滤
	meta[AREA.srcSearch].items.map((item) => {
		if (item.attrcode === FIELDS.pk_org) {
			setRefMainOrgPermissonByItem(item);
		} else if (item.attrcode == FIELDS.priceaudit_material) {
			// 物料
			item.queryCondition = () => {
				let pk_org = (props.search.getSearchValByField(AREA.srcSearch, FIELDS.pk_org) || {}).value;
				return {
					pk_org: (pk_org || {}).firstvalue,
					SCM_DISCOUNTFLAG: 'N',
					SCM_FEEFLAG: 'N',
					GridRefActionExt: 'nccloud.web.scmpub.ref.MaterialRefFilterUtils'
				};
			};
		} else if (item.attrcode === FIELDS.depid) {
			// 采购部门
			item.queryCondition = () => {
				let pk_org = (props.search.getSearchValByField(AREA.srcSearch, FIELDS.pk_org) || {}).value;
				return {
					pk_org: (pk_org || {}).firstvalue,
					busifuncode: 'pu'
				};
			};
		} else if (item.attrcode === FIELDS.pk_bizpsn) {
			// 采购员
			item.isShowUnit = false;
			item.queryCondition = () => {
				let pk_org = (props.search.getSearchValByField(AREA.srcSearch, FIELDS.pk_org) || {}).value;
				let pk_dept = (props.search.getSearchValByField(AREA.srcSearch, FIELDS.depid) || {}).value;
				return {
					pk_org: (pk_org || {}).firstvalue,
					pk_dept: (pk_dept || {}).firstvalue,
					busifuncode: 'pu'
				};
			};
		} else if (item.attrcode.match('vbdef') || item.attrcode.match('vfree') || item.attrcode.match('vdef')) {
			//自定义项，自由辅助属性
			item.queryCondition = () => {
				let data = props.search.getSearchValByField(AREA.srcSearch, FIELDS.pk_org);
				// 财务组织单选时，进行过滤
				if (data && data.value.firstvalue.indexOf(',') == -1) {
					return { pk_org: data.value.firstvalue };
				} else {
					return {};
				}
			};
		} else {
			item.queryCondition = () => {
				let pk_org = (props.search.getSearchValByField(AREA.srcSearch, FIELDS.pk_org) || {}).value;
				return {
					pk_org: (pk_org || {}).firstvalue
				};
			};
		}

		// 设置显示停用、人员显示离职
		setRefShowDisabledData(item);
		setPsndocShowLeavePower(item);
	});

	//单据号列加超链接
	meta[AREA.srcHead].items.map((item, key) => {
		transferSkipToSrcBillUtil.call(this, props, item, {
			billtype: '28',
			billcodefield: FIELDS.vbillcode,
			pkfield: FIELDS.pk_priceaudit
		});
	});

	return meta;
}

/*pmFWCFu5nhKkBzYmrkBakYsZp5swriLu08OGCHqhiBP8j8oYSTPG1fSFojrU7cKi*/